;
$(function () {
    var ad = appData(),
        tmpObj = {},
        menuData;
    var jump = $.getUrlParam("page")

    if (jump == "views/safeApp/homePage/jumpHtml") {
        $('html').hide()
        return $('#sys-main').empty().load(jump + ".html");
    }
    /*var jumpUrl = {
        "homePage": 2,
        "dataRegulation": 0,
        "logManage": 1,
        "sysManage": 3,
        "trafficAnalysis": 0
    }*/
    var jumpUrl = {
    }
    var showT = {
        数据监管: 'dataRegulation',
        日志管理: 'logManage',
        系统管理: 'sysManage',
        设备管理: 'homePage',

    }

    var jumpUrlName = {}

    $(function () {

        getTemp();

        showMenu();

        setImageUesr()

        $("#sys-menu").on("click", "a", clickTopMenu);

        $("#secondMenu").on('click', "a", clickLeftMenu);

        $("#logOut").on("click", function () {
            window.location.href = appApi("LOGOUT");
            ;
        })
        $(".toggle-sidebar i").off('click').on('click', function () {
            if ($(this).hasClass("icon-toleft")) {
                $(this).prop("class", "icon-toright");
                $(".sys-main").stop(true).animate({"marginLeft": 50}, 400);
                $(".sys-sidebar").stop(true).animate({"width": 50}, 400).addClass('hide');
            }
            else {
                $(this).prop("class", "icon-toleft");
                $(".sys-main").stop(true).animate({"marginLeft": 240}, 400);
                $(".sys-sidebar").stop(true).animate({"width": 240}, 400).removeClass('hide');
            }
        });
    })

    function showMenu() {
        ad.selectMenu("KMGA_MENU", {}, function (data) {
            menuData = data;
            data.forEach(function (d, i) {
                jumpUrlName[`${showT[`${d.name}`]}`] = i
            })
            jumpUrlName.trafficAnalysis=jumpUrlName.dataRegulation
            jumpUrl=jumpUrlName
            $.tmpl(tmpObj.newLeft, data).appendTo("#sys-menu ul");
            //移动游标
            $(".sys-sidebar-menu").on("mouseover", "li", function () {
                var obj = $(this);
                $(".curLineY").stop(true).animate({
                    top: obj.position().top,
                }, 200, function () {
                    obj.siblings().find('span').stop(true).hide();
                    obj.find('span').stop(true).show();
                });

            })


            $(".sys-sidebar-menu").on("mouseout", "li", function () {
                var obj = $(this);
                $(".curLineY").animate({
                    top: obj.siblings('.active').position() ? obj.siblings('.active').position().top : obj.position().top
                }, 200, function () {
                    $("#sys-menu  li span").hide();
                })
            })
            if (jump) {
                var str = jump.split('/')[2]
                $("#sys-menu").find("a:eq(" + jumpUrl[str] + ")").click();

            } else {
                $("#sys-menu").find("a:eq(0)").click();
            }
        });
    }

    function clickTopMenu() {
        var jump = $.getUrlParam("page")
        var $this = $(this),
            url = $this.attr("data-url");
        $('.sys-sidebar-submenu h2').text(this.text)
        $(this.parentElement).siblings().removeClass('active')
        $this.parent('li').addClass("active");

        $(".curLineY").stop(true).animate({
            top: $this.parent('li').position().top,
        }, 200);


        $(".curLine").css({
            "width": $("#sys-menu ul li.active").width(),
            "left": $("#sys-menu ul li.active").position().left
        });
       // $('#sys-main').empty().load(url + ".html");
        menuData.forEach(function (d, i) {
            if (url == d.url) {
                $("#secondMenu").empty();
                $.tmpl(tmpObj.leftMenu, d).appendTo("#secondMenu");
                if (jump) {
                    $("#secondMenu  a").each(function (h, i) {
                        if ($(this).attr("data-url") == jump && h != 0) {
                            return $("#secondMenu a:eq(" + h + ")").click();
                        }
                    })
                } else {
                    if (d.name == '设备管理') {
                        $("#secondMenu a:eq(0)").click();
                    } else {
                        $("#secondMenu a:eq(1)").click();
                    }
                }
            }
        })

        $(".sys-sidebar em").on("click", function () {
            if ($(this).hasClass('open')) {
                $(this).attr("class", 'hide');
                $(this).next("ul").slideUp();
            }
            else {
                $(this).attr("class", 'open');
                $(this).next("ul").slideDown();
            }
        })
    }

    function clickLeftMenu() {
        var childUrl = $(this).attr("data-url");
        if (childUrl == '') {
            return
        }
        $('#sys-main').empty().load(childUrl + ".html");
        $('#secondMenu li').removeClass("active");
        $(this).parent('li').addClass("active");
        $(this).parent('li').parent().parent().addClass("active");
    }

    function getTemp() {
        tmpObj.menu = "<li><a href=\"javascript:void(0);\" data-url=\"{{= url}}\">{{= name}}</a></li>";
        tmpObj.newLeft = "<li class=\"\"> <a data-url=\"{{= url}}\" href=\"javascript:void(0);\"><i class=\"{{= icon}}\"></i><span style=\"display: none;\">{{= name}}</span></a></li>";
        // tmpObj.leftMenu = "{{each(i,data) child}}\n<li class=\"\"><a href=\"javascript:void(0);\" data-url=\"{{= data.url}}\"><i class=\"{{= data.icon }}\"></i><span>{{= data.name}}</span></a>\n    {{if data.childs.length>0}}\n <em class=\"open\"></em>   <ul>\n        {{each(i,data) childs}}\n        <li class=\"\"><a href=\"javascript:void(0);\" data-url=\"{{= data.url}}\"><i class=\"{{= data.icon }}\"></i><span>{{=\n            data.name}}</span></a></li>\n        {{/each}}\n    </ul>\n    {{/if}}\n</li>\n{{/each}}";
        tmpObj.leftMenu = "{{each(i,data) childs}}\n<li class=\"\"><a href=\"javascript:void(0);\" data-url=\"{{= data.url}}\"><i class=\"{{= data.icon }}\"></i><span>{{= data.name}}</span></a>\n    {{if data.childs.length>0}}\n  <em class=\"open\"></em>   <ul>\n        {{each(i,data) childs}}\n        <li class=\"\"><a href=\"javascript:void(0);\" data-url=\"{{= data.url}}\"><i class=\"{{= data.icon }}\"></i>{{=\n            data.name}}</a>\n            {{if data.childs.length>0}}\n            <ul class=\"lastmenu\">\n                {{each(i,data) childs}}\n                <li class=\"\"><a href=\"javascript:void(0);\" data-url=\"{{= data.url}}\">{{= data.name}}</a></li>\n                {{/each}}\n            </ul>\n            {{/if}}\n        </li>\n        {{/each}}\n    </ul>\n    {{/if}}\n</li>\n{{/each}}";
    };

    //用户头像
    function setImageUesr() {

        ad.selectList('SELECT_USER', {}, function (data) {
            $('#uersName').html(data)
        })

    }
})